package com.ztgf.sys.api.cms;

import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.bo.HeadParamsBO;
import com.ztgf.common.constant.CommonParameter;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.shopping.models.service.shop.IShopSysInvoiceService;
import com.ztgf.shopping.models.vo.shop.ShopSysInvoiceRelevanceVO;
import com.ztgf.sys.models.bo.SysInvoiceQueryBO;
import com.ztgf.sys.models.bo.SysInvoiceSaveBO;
import com.ztgf.sys.models.dto.SysConfigDTO;
import com.ztgf.sys.models.dto.SysInvoiceDTO;
import com.ztgf.sys.models.service.ISysConfigService;
import com.ztgf.sys.models.service.ISysInvoiceService;
import com.ztgf.sys.models.vo.SysInvoiceQueryVO;
import com.ztgf.sys.models.vo.SysInvoiceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * 平台开票控制器
 *
 * @author liyang
 * @Date 2021-05-26 11:50:37
 */

@RestController
@RequestMapping("/cms/sysInvoice")
@Slf4j
@Api(value = "SysInvoiceController",tags = "平台开票接口")
public class SysInvoiceController {

    @DubboReference
    private ISysInvoiceService sysInvoiceService;
    @DubboReference
    private IShopSysInvoiceService shopSysInvoiceService;
    @DubboReference
    private ISysConfigService sysConfigService;


    @ApiOperation(value = "平台开票",notes = "平台开票")
    @PostMapping("/updateSysInvoice")
    public Result updateSysInvoice(SysInvoiceSaveBO saveBO){
        if (saveBO == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }else {
            HeadParamsBO headParamsBO = CommonParameter.getHeadParamsBO();
            boolean flag = sysInvoiceService.updateSysInvoice(saveBO,headParamsBO.getSessionId(),headParamsBO.getSource().toString());
            if (flag){
                return new Result<>();
            } else {
                return new Result<>(CodeEnum.FAIL);
            }
        }
    }

    @PostMapping("/get")
    @ApiOperation(value = "开票详情",notes = "开票详情")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<SysInvoiceQueryVO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        SysInvoiceQueryVO sysInvoiceQueryVO = new SysInvoiceQueryVO ();
        SysInvoiceDTO dto = sysInvoiceService.selectById(id);
        List<ShopSysInvoiceRelevanceVO> shopSysInvoiceRelevanceVOS = shopSysInvoiceService.selectVOS (id);
        sysInvoiceQueryVO.setSysInvoiceDTO (dto);
        sysInvoiceQueryVO.setShopSysInvoiceRelevanceVOS (shopSysInvoiceRelevanceVOS);
        return new Result<>(sysInvoiceQueryVO);
    }

    @PostMapping(value = "/updateById")
    @ApiOperation(value = "根据id更新发票数据", notes = "根据id更新发票数据")
    public Result updateById(SysInvoiceDTO sysInvoiceDTO) {
        if (sysInvoiceDTO == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        } else {
            boolean a=sysInvoiceService.updateSysInvoiceById(sysInvoiceDTO);
            if (a) {
                return new Result<>();
            } else {
                return new Result<>(CodeEnum.FAIL_100011);
            }
        }
    }

    @ApiOperation(value = "平台开票列表和开票记录",notes = "平台开票列表和开票记录")
    @PostMapping("/queryPageList")
    public Result<Page<SysInvoiceVO>> queryPageList(SysInvoiceQueryBO bo){
        return new Result<> (sysInvoiceService.queryPageList(bo));
    }

    @ApiOperation(value = "设置开票最低金额",notes = "设置开票最低金额")
    @PostMapping("/updateInvoicePriceMin")
    public Result updateInvoicePriceMin(BigDecimal minPrice){
//        if (minPrice.compareTo (new BigDecimal (0)) < 0){
//            return new Result<>(CodeEnum.FAIL_100002 );
//        }
        SysConfigDTO sysConfigDTO = new SysConfigDTO ();
        sysConfigDTO.setId (2L);
        sysConfigDTO.setValue (minPrice.toString ());
        boolean flag = sysConfigService.updateById (sysConfigDTO);
        if (flag){
            return new Result<>(CodeEnum.SUCCESS );
        }else {
            return new Result<>(CodeEnum.FAIL );
        }
    }

    @ApiOperation(value = "呈现开票最低金额",notes = "呈现开票最低金额")
    @PostMapping("/getInvoicePriceMin")
    public Result getInvoicePriceMin(){
        SysConfigDTO sysConfigDTO = sysConfigService.selectById (2);
        if (sysConfigDTO != null){
            return new Result<>(new BigDecimal (sysConfigDTO.getValue ()) );
        }else {
            return new Result<>(CodeEnum.FAIL_100002 );
        }
    }
}
